package com.hyf.treeview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hyf.treeview.entity.Employee;
import com.hyf.treeview.support.TreeNode;
import com.hyf.treeview.support.TreeNodeRecyclerAdapter;
import com.hyf.treeview.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<TreeNode> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        List<Employee> employeeList = AppUtil.getEmployeeData(this);
        for (Employee e : employeeList) {
            TreeNode treeNode = new TreeNode(e.isBm() ? R.mipmap.tree_view_group : R.mipmap.tree_view_user, e.getId(), e.getpId(), e.getName() == null ? e.getRealname() : e.getName(), e);
            treeNode.setLeaf(!e.isBm());
            datas.add(treeNode);
        }

        RecyclerView recyclerView = findViewById(R.id.rv_main);
        recyclerView.setVisibility(View.VISIBLE);
        try {
            final TreeNodeRecyclerAdapter treeListViewAdapter = new TreeNodeRecyclerAdapter(this, datas, onTreeNodeSigleClickListener);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(treeListViewAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TreeNodeRecyclerAdapter.OnTreeNodeMultiClickListener onTreeNodeMultiClickListener = new TreeNodeRecyclerAdapter.OnTreeNodeMultiClickListener() {
        @Override
        public void onMultiClick(List<TreeNode> selectList, TreeNode treeNode, int visablePositon, int realDatasPositon, boolean isLeaf) {
            Log.e("选中数量>>>>>", selectList.size() + "");
            Log.e("选中的集合>>>>>", selectList.toString());
        }
    };

    private TreeNodeRecyclerAdapter.OnTreeNodeSigleClickListener onTreeNodeSigleClickListener = new TreeNodeRecyclerAdapter.OnTreeNodeSigleClickListener() {
        @Override
        public void onSigleClick(TreeNode treeNode, int visablePositon, int realDatasPositon, boolean isLeaf) {
            if (isLeaf)
                Toast.makeText(MainActivity.this, treeNode.getLabel() + ">>>" + treeNode.getObject().toString(), Toast.LENGTH_SHORT).show();
        }
    };
}
